//
//  DetailViewController.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#define WIDTH  [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height


#import "DetailViewController.h"
#import "DetailView.h"
#import "LoginViewController.h"
#import "Activity.h"
#import "ActivitySinglton.h"

@interface DetailViewController ()
@property (nonatomic,retain) DetailView *detailView;
@property (nonatomic,retain) NSMutableArray *titleArray;
@end

@implementation DetailViewController
- (void)dealloc
{
    
    [_titleArray release];
    [_detailView release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
#warning 按钮写在这里不好吧
    //收藏按钮
    self.starbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.starbutton.frame = CGRectMake(165, 180, 20, 20);
    //初始化标题数组
    self.titleArray = [NSMutableArray array];
    
    //打开数据库
    [[ActivitySinglton shareDataBaseSingleton] openDB];
    //    [[ActivitySinglton shareDataBaseSingleton] createCollectionTable];
    
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    //登陆的
    if (name.length != 0){
        NSMutableArray *array = [[ActivitySinglton shareDataBaseSingleton] selectCollectionWithName:name];
        //存储title
        for (Activity *activity in array) {
            [self.titleArray addObject:activity.title];
        }
        
        if ([self.titleArray containsObject:self.activity.title]) {
            UIImage *collectionImage = [UIImage imageNamed:@"ic_star_red@2x.png"];
            collectionImage = [collectionImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.starbutton setBackgroundImage:collectionImage forState:UIControlStateNormal];
        }
        else{
            UIImage *collectionImage = [UIImage imageNamed:@"ic_star_half@2x.png"];
            collectionImage = [collectionImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.starbutton setBackgroundImage:collectionImage forState:UIControlStateNormal];
        }
        
    }
    else
    {
        UIImage *collectionImage = [UIImage imageNamed:@"ic_star_half@2x.png"];
        collectionImage = [collectionImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.starbutton setBackgroundImage:collectionImage forState:UIControlStateNormal];
    }
    //别用错方法了!
    //    [self.starbutton setBackgroundImage:[UIImage imageNamed:@"ic_star_red@2x"] forState:UIControlStateSelected];
    [self.starbutton addTarget:self action:@selector(buttonAlert:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailView addSubview:self.starbutton]; //加在controller没有用
    

}




- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.view.backgroundColor = [UIColor purpleColor];
    UIImageView *bgimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3.jpg"]];
    [self.view addSubview:bgimageView];
    //这个方法应该先声明好了 再在这里调用的
    self.detailView = [[DetailView alloc] initWithFrame:[[UIScreen mainScreen] bounds] Activity:self.activity];
    
    
    
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //固定的不好吧!!?!改好了!!!
    CGFloat newHeight = [DetailView heightForContent:self.activity.content] ;
    scrollView.contentSize = CGSizeMake(WIDTH, newHeight + 430);
    [scrollView addSubview:self.detailView];
    [self.view addSubview:scrollView];
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x"];
    //取消渲染
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction:)];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share@2x"];
    //取消渲染
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
    
    [_detailView release];
    [scrollView release];
    
    
    // Do any additional setup after loading the view.
}
//收藏判断
-(void)buttonAlert:(UIButton *)button{

    [[ActivitySinglton shareDataBaseSingleton] openDB];
    [[ActivitySinglton shareDataBaseSingleton] createCollectionTable];
#warning !!!!!
    //完整下将要插入的模型
    self.activity.name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    //登陆的
    if (name.length != 0){
        NSMutableArray *array = [[ActivitySinglton shareDataBaseSingleton] selectCollectionWithName:name];
        //存储title
        for (Activity *activity in array) {
            [self.titleArray addObject:activity.title];
        }
        
        if ([self.titleArray containsObject:self.activity.title]) {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您已经收藏过此电影" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
            
        }
        else{
            UIImage *collectionImage = [UIImage imageNamed:@"ic_star_red@2x.png"];
            collectionImage = [collectionImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [self.starbutton setBackgroundImage:collectionImage forState:UIControlStateNormal];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜您成功收藏此电影" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
            
            
            [[ActivitySinglton shareDataBaseSingleton] insertCollectionTable:self.activity];
        }
        
    }
    
    //未登录的
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您还没有登录,收藏失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
        
        UIImage *collectionImage = [UIImage imageNamed:@"ic_star_half@2x.png"];
        collectionImage = [collectionImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.starbutton setBackgroundImage:collectionImage forState:UIControlStateNormal];
        
    }
    
    
}

//左侧按钮
-(void)leftBarAction:(UIButton *)barbutton{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//右侧按钮
-(void)rightBarAction:(UIBarButtonItem *)barbutton{
    //    NSLog(@"djokp");
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"login"] isEqualToString:@"1"]) {
#warning 我忘了
        UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"分享成功" preferredStyle:UIAlertControllerStyleAlert];
        
        [Alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:Alert animated:YES completion:^{
        }];
        
    }else{
        LoginViewController *loginViewVC = [[LoginViewController alloc] init];
        //模态要重建UINavigation..
        UINavigationController *loginViewnaC = [[UINavigationController alloc] initWithRootViewController:loginViewVC];
        [self presentViewController:loginViewnaC animated:YES completion:^{
        }];
        [loginViewnaC release];
        [loginViewVC release];
        
    }
    
}

//收藏(未实现记录和判断是否登录)
-(void)buttonAction:(UIButton *)button{
    //必写
    button.selected = !button.selected;
    //提示框
    UIAlertController *starAlert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertController *starAlert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"取消收藏" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confilm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [starAlert1 addAction:confilm];
    [starAlert2 addAction:confilm];
    
    if (button.selected == YES) {
        [self presentViewController:starAlert1 animated:YES completion:^{
        }];
        
    }else{
        [self presentViewController:starAlert2 animated:YES completion:^{
        }];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
